package dao

import (
	"errors"
	"gbox-one-machine/client"
	"gbox-one-machine/conf"
	"github.com/go-resty/resty/v2"
)

// reusable
var conn *Conn

// errors
var (
	ErrConnNotExists = errors.New("Conn does not exists")
)

// Conn dao conn
type Conn struct {
	httpConn *resty.Client
}

// GetConn get conn singleton
func GetConn(cf *conf.Dao) (*Conn, error) {
	if conn != nil {
		return conn, nil
	}

	return NewConn(cf)
}

// NewConn create conn
func NewConn(cf *conf.Dao) (*Conn, error) {

	conn = &Conn{
		httpConn: NewHTTPConn(),
	}

	return conn, nil
}

// NewHTTPConn new consul conn
func NewHTTPConn() *resty.Client {
	return client.GetHTTPClient()
}
